//
//  YQPAdvisorySelectTypeView.m
//  PensionDoctor
//
//  Created by tbb on 2019/7/10.
//  Copyright © 2019 YQ. All rights reserved.
//

#import "YQPAdvisorySelectTypeView.h"

@interface YQPAdvisorySelectTypeView ()<UITableViewDelegate, UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, copy) YQPAdvisorySelectResultBlack resultBlack;
@property (nonatomic, copy) YQPAdvisorySelectCancelBlack cancelBlack;

@property (nonatomic, strong) YQBaseTableView * selectTableView;

@property (nonatomic, strong) NSMutableArray * titleArr;
@property (nonatomic, copy) NSString * selectStr;
@end

@implementation YQPAdvisorySelectTypeView

-(instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource selectStr:(NSString *)selectStr resultBlack:(YQPAdvisorySelectResultBlack)resultBlack cancelBlack:(YQPAdvisorySelectCancelBlack)cancelBlack {
    if (self = [super initWithFrame:frame]) {
        self.titleArr = dataSource.copy;
        self.selectStr = selectStr.copy;
        self.backgroundColor = HexColorA(0x000000, 0.2);//HexColor(0xF3F5F9);
        [self addSubview:self.selectTableView];
        self.resultBlack = resultBlack;
        self.cancelBlack = cancelBlack;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelSelctTableView)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
    }
    return self;
}


-(void)showSelctTableView {
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    // 浮现动画
    [UIView animateWithDuration:0.3 animations:^{
        self.selectTableView.height = self.titleArr.count * 44;//347;
    }];
    
}

-(void)cancelSelctTableView {
    
    self.cancelBlack();
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        self.selectTableView.height = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPAdvisorySelectTypeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YQPAdvisorySelectTypeViewCell"];
    cell.textLabel.text = self.titleArr[indexPath.row];
    NSInteger index = [self.titleArr indexOfObject:self.selectStr];
    cell.iconBtn.selected = NO;
    if (indexPath.row == index) {
        cell.iconBtn.selected = YES;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    YQPAdvisorySelectTypeViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    cell.iconBtn.selected = YES;

    [self.selectTableView deselectRowAtIndexPath:indexPath animated:true];
    self.resultBlack(self.titleArr[indexPath.row],indexPath.row);
    [self cancelSelctTableView];

}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *bgView = [UIView new];
    bgView.backgroundColor = UIColor.whiteColor;
    return bgView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end




-(YQBaseTableView *)selectTableView {
    if (!_selectTableView) {
        _selectTableView = [[YQBaseTableView alloc] initWithFrame:CGRectMake(0, 0, kYQPScreenWidth, 0) style:(UITableViewStylePlain)];
        _selectTableView.backgroundColor = UIColor.whiteColor;
        _selectTableView.delegate = self;
        _selectTableView.dataSource = self;

        [_selectTableView registerClass:[YQPAdvisorySelectTypeViewCell class] forCellReuseIdentifier:@"YQPAdvisorySelectTypeViewCell"];
    }
    return _selectTableView;
}
-(NSMutableArray *)titleArr {
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UITableViewCellContentView（即点击了tableViewCell），
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        // cell 不需要响应 父视图的手势，保证didselect 可以正常
        return NO;
    }
    //默认都需要响应
    return  YES;
}


@end

